<?php

/**
 * Created by PhpStorm.
 * User: Evan
 * Date: 16/3/17
 * Time: 下午6:17
 */
class Public_cate_feature extends MY_Agent_Controller
{
    public function index()
    {
        $limit   = 20;
        $curPage = (int)$this->input->get_post('per_page');
        $offset  = ($curPage < 1) ? 0 : ($curPage - 1) * $limit;


        $name = trim($this->input->get_post('name'));
        $cid  = $this->input->get_post('cid');
        $time = $this->input->get_post('create_time');


        $where = array();
        $name  && $where['name like \'%'.$name.'%\''] = null;
        ($cid || $cid == '0')   && $where['cid'] = (int)$cid;
        if ($time)
        {
            $eTime = explode('-', $time);
            $where['create_time >='] = date('Y-m-d H:i:s', strtotime($eTime[0]));
            $where['create_time <='] = date('Y-m-d ', strtotime($eTime[1])).'23:59:59';
        }


        $this->load->model('Model_public_cate', 'pc');
        $this->load->model('Model_public_cate_feature', 'pcf');

        $totalNum = $this->pcf->count();
        $info = $this->pcf->lists($offset, $limit, $where);


        $this->load->library('pagination');
        $config = $this->page_config('Public_cate_feature/index', $totalNum, $limit);
        $this->pagination->initialize($config);
        $pageHtml = $this->pagination->create_links();


        $cate = $this->pc->lists(0, 1000, array(), true, 'cid asc');

        $data = array(
            'title'    => '公众号分类特征库列表',
            'info'     => $info,
            'name'     => $name,
            'time'     => $time,
            'cid'      => $cid,
            'cate'     => sortData($cate),
            'pageHtml' => $pageHtml,
            'totalNum' => $totalNum,
        );

        $this->load->view('agent/public/cate_feature_index', $data);
    }


    /**
     * 保存
     */
    public function save()
    {
        $response = array('code' => '0', 'msg' => '保存成功');

        do {
            $fid = (int)$this->input->get_post('fid');

            $name = trim($this->input->get_post('name'));
            $cid  = (int)$this->input->get_post('cid');


            $data = array();
            !empty($name) && $data['name'] = $name;
            $cid && $data['cid'] = $cid;


            $this->load->model('Model_public_cate_feature', 'pcf');


            if ($fid)
            {
                $item = $this->pcf->getById($fid);
                if (empty($item))
                {
                    $response = error(300011);
                    break;
                }
            }
            else
            {
                $item = $this->pcf->getOne(array('cid' => $cid, 'name' => $name));
                if (!empty($item))
                {
                    $response = error(300020);
                    break;
                }
            }


            if (empty($data))
            {
                $response = error(300013);
                break;
            }




            $res = $this->pcf->save($data, $fid ? $fid : null);
            if (!$res)
            {
                $response = error(300014);
                break;
            }
        }while (false);


        $this->json_output($response);
    }


    /**
     * 删除
     */
    public function delete()
    {
        $response = array('code' => '0', 'msg' => '删除成功');

        do {
            $fid = (int)$this->input->get_post('fid');

            if (!$fid)
            {
                $response = error(100000);
                break;
            }


            $this->load->model('Model_public_cate_feature', 'pcf');
            $item = $this->pcf->getById($fid);

            if (empty ($item))
            {
                $response = error(300011);
                break;
            }


            $res = $this->pcf->delete(array('fid' => $fid));
            if (!$res)
            {
                $response = error(300012);
                break;
            }
        }while (false);


        $this->json_output($response);
    }
}